package co.beto.commons
{
  import mx.controls.Alert;
  import mx.core.UIComponent;
 
  public class DashBorderInSide extends UIComponent{
    private var dashlen:Number = 2;
    private var gaplen:Number = 3;
   
    public function DashBorderInSide(){
      super();
    }

    override protected function updateDisplayList
      (unscaledWidth:Number, unscaledHeight:Number):void{

      super.updateDisplayList(unscaledWidth, unscaledHeight);
      graphics.clear();
	  //BackgroundColor
      graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
      
      if (getStyle("dashlen")) {
           this.dashlen = getStyle("dashlen");
      }
      if (getStyle("gaplen")) {
           this.gaplen = getStyle("gaplen");
      }

      drawBorder(this.x, this.y, unscaledWidth, unscaledHeight,
        this.dashlen, this.gaplen);
    }

    public function drawLine(x1:Number, y1:Number, x2:Number, y2:Number,
      dashlen:Number, gaplen:Number): void {    
      if((x1 != x2) || (y1 != y2)){
        var incrlen:Number = dashlen + gaplen;
     
        var len:Number = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        var angle:Number = Math.atan((y2 - y1) / (x2 - x1));
        var steps:uint = len / (dashlen + gaplen);
     
        var dashstepx:Number = dashlen * Math.cos(angle);
        if (x2 < x1) dashstepx *= -1;
     
        var dashstepy:Number = dashlen * Math.sin(angle);
     
        var gapstepx:Number = gaplen * Math.cos(angle);
        if (x2 < x1) gapstepx *= -1;
     
        var gapstepy:Number = gaplen * Math.sin(angle);
        var stepcount:uint = 0;
     
        while (stepcount < steps) {       

          var dashstartx:Number;
          var dashstarty:Number;
          var dashendx:Number;
          var dashendy:Number;
       
          if(x1 == x2 && y1 != y2){
          	stepcount++;
            dashstartx = dashendx = x1;
            if(y2 > y1){
              dashstarty = y1 + ((stepcount-1) * (dashlen + gaplen));             
              dashendy = dashstarty + dashlen;
            }else{
              dashstarty = y1 - ((stepcount-1) * (dashlen + gaplen));             
              dashendy = dashstarty - dashlen;
            }
            graphics.moveTo(dashstartx, dashstarty);
            /*Change BorderThickness, Color or BackgrondAlpha here
             *lineStyle(BorderThickness, Color, BackgroundAlpha)
             */
            graphics.lineStyle(2,0,1,false,"normal","none");
            if(y2>y1){
            	graphics.lineTo(dashendx, dashendy);
            }else{
            	graphics.lineTo(dashendx, dashendy);
            }
            
            
            //Draw next line with alternative color
            stepcount++;
            if(y2 > y1){
              dashstarty = y1 + ((stepcount-1) * (dashlen + gaplen));             
              dashendy = dashstarty + dashlen;
            }else{
              dashstarty = y1 - ((stepcount-1) * (dashlen + gaplen));             
              dashendy = dashstarty - dashlen;
            }
            graphics.moveTo(dashstartx, dashstarty);
            /*Change BorderThickness, Color or BackgrondAlpha here
             *lineStyle(BorderThickness, Color, BackgroundAlpha)
             */
            graphics.lineStyle(2,0xB28000,1,false,"normal","none");
            if(y2>y1){
            	graphics.lineTo(dashendx, dashendy);
            }else{
            	graphics.lineTo(dashendx, dashendy);
            }
            
          }else if(y1 == y2 && x1 != x2){
          	stepcount++;
            dashstarty = dashendy = y1;
            if(x2 > x1){
              dashstartx = x1 + ((stepcount-1) * (dashlen + gaplen));
              dashendx = dashstartx + dashlen;
            }else{
              dashstartx = x1 - ((stepcount-1) * (dashlen + gaplen));
              dashendx = dashstartx - dashlen;
            }
            graphics.moveTo(dashstartx, dashstarty);            
            /*Change BorderThickness, Color or BackgrondAlpha here
             *lineStyle(BorderThickness, Color, BackgroundAlpha)
             */
            graphics.lineStyle(2,0,1,false,"normal","none");
            if(x2>x1){
            	graphics.lineTo(dashendx, dashendy);
            }else{
            	graphics.lineTo(dashendx, dashendy);
            }
            
            
            
            //Draw next line with alternative color
            stepcount++;
            if(x2 > x1){
              dashstartx = x1 + ((stepcount-1) * (dashlen + gaplen));
              dashendx = dashstartx + dashlen;
            }else{
              dashstartx = x1 - ((stepcount-1) * (dashlen + gaplen));
              dashendx = dashstartx - dashlen;
            }
            graphics.moveTo(dashstartx, dashstarty);
            
            /*Change BorderThickness, Color or BackgrondAlpha here
             *lineStyle(BorderThickness, Color, BackgroundAlpha)
             */
            graphics.lineStyle(3,0xB28000,1,false,"normal","none");
            if(x2>x1){
            	graphics.lineTo(dashendx, dashendy);
            }else{
            	graphics.lineTo(dashendx, dashendy);
            }
            
          }
          
        }
      }
    }
   
    private function drawBorder(x1:Number, y1:Number, width:Number, height:Number,
      dashlen:Number, gaplen:Number) : void {
      drawLine(x1, y1, x1 + width, y1, dashlen, gaplen);
      drawLine(x1 + width, y1+4, x1 + width, y1 + height, dashlen, gaplen);
      drawLine(x1 + width, y1 + height, x1, y1 + height, dashlen, gaplen);
      drawLine(x1, y1 + height-4, x1, y1, dashlen, gaplen);
    }   
  }
}
